package com.zhangjinghao.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.zhangjinghao.entity.*;
import com.zhangjinghao.mapper.DesireMapper;
import com.zhangjinghao.mapper.EducationMapper;
import com.zhangjinghao.service.DesireService;
import com.zhangjinghao.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("resume")
public class ResumeController {

    @Autowired
    private ResumeService resumeService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private DesireService desireService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private EducationMapper educationMapper;

    /**
     * 前往列表
     * @param selectArg
     * @return
     */
    @RequestMapping("list")
    public String list(SelectArg selectArg){

        PageInfo<Resume> pageInfo = null;
        List<Desire> desire = null;

        //预热页面
        if (!redisTemplate.hasKey("page1")){
            pageInfo = resumeService.pageInfo(new SelectArg(1, 3));
            redisTemplate.opsForValue().set("page1",pageInfo,new Random().nextInt(1000)+1000, TimeUnit.MINUTES);
        }

        //缓存意象
        if (!redisTemplate.hasKey("desire")){
            desire = desireService.selectList();
            redisTemplate.opsForValue().set("desire",desire,new Random().nextInt(1000)+1000, TimeUnit.MINUTES);
        }else{
            desire = (List<Desire>) redisTemplate.opsForValue().get("desire");
        }
        request.getSession().setAttribute("DESIRE",desire);

        //缓存页面
        if (!redisTemplate.hasKey("page"+selectArg.getPageNum().toString())){
            pageInfo = resumeService.pageInfo(selectArg);
        }else{
            pageInfo = (PageInfo<Resume>) redisTemplate.opsForValue().get("page"+selectArg.getPageNum().toString());
        }
        redisTemplate.opsForValue().set("page"+selectArg.getPageNum().toString(),pageInfo,new Random().nextInt(1000)+1000, TimeUnit.MINUTES);

        request.setAttribute("list",pageInfo);
        return "resumer-list";
    }

    /**
     * 前往添加页面
     * @return
     */
    @RequestMapping("toInsert")
    public String toInsert(){
        if (request.getSession().getAttribute("DESIRE")==null){
            request.getSession().setAttribute("DESIRE",redisTemplate.opsForValue().get("desire"));
        }
        List<Education> educations = null;
        if (!redisTemplate.hasKey("education")){
            educations = educationMapper.selectList();
            redisTemplate.opsForValue().set("education",educations,new Random().nextInt(1000)+1000, TimeUnit.MINUTES);
        }else{
            educations = (List<Education>) redisTemplate.opsForValue().get("education");
        }
        request.setAttribute("educations", JSON.toJSON(educations));
        request.setAttribute("genders",Gender.values());
        redisTemplate.opsForValue().set("flag-resumer","",new Random().nextInt(1000)+1000,TimeUnit.MINUTES);
        return "resumer-insert";
    }

    /**
     * 执行添加
     * @param resume
     * @return
     */
    @RequestMapping("insert")
    @ResponseBody
    public int insert(Resume resume){

        if (!redisTemplate.hasKey("flag-resumer")){
            return 0;
        }
        int insert = resumeService.insert(resume);
        if (insert > 0){
            redisTemplate.delete(redisTemplate.keys("*"));
        }
        return insert;
    }
}
